<template>
  <div>
    <el-dialog title="分配角色" :visible="showAssignRole" @close="cancelBtn">
      <el-form :model="form">
        <el-form-item prop="role">
          <el-checkbox-group
            v-model="form.role"
             @change="handleCheckedsChange"
          >
            <el-checkbox v-for="role in roleList" :key="role.id" :label="role.id">{{role.name}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="btnOk">确定</el-button>
          <el-button @click="cancelBtn">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {getUserDetailById} from "@/api/user"
import {assignRoles} from "@/api/employees"
import {getRoleList,getRoleDetail} from "@/api/setting"
export default {
  name: "assignRole",
  props: ["showAssignRole","userId"],
  data(){
    return {
      roleList:[],
      form:{
        role:[],
      }
    }
  },
  methods:{
    cancelBtn(){
      this.$emit("update:showAssignRole",false);
      this.form.role = [];
    },
    async getRoleList(){
      let result = await getRoleList();
      console.log(result);
      this.roleList = result.rows;
    },
    async getRole(id){
      let result = await getUserDetailById(id);
      console.log(result,"role");
      this.form.role = result.roleIds || [];
    },
    async btnOk(){
      try {
        let result = await assignRoles({id:this.userId,roleIds:this.form.role});
        this.$message({
          type:"success",
          message:"分配角色成功！",
        })
      } catch (error) {
        this.$message({
          type:"info",
          message:"分配角色失败！",
        })
      }
      this.$emit("update:showAssignRole",false);
    },
    handleCheckedsChange(e){
      // console.log(e,"e");
      // console.log(this.form.role,"role");
    },

  }
};

</script>

<style>
</style>